package app;

import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

public class Management {
    static Scanner sc=new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        registration();
//        login();
    }
    public static void registration() throws IOException {
        String username = "";
        String password="";
        String gender  ="";
        String  contactInformation=""  ;
        String signature="";
        User user=new User();
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();


        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        while (true) {
            System.out.println("🐵请输入用户名：");
            username= sc.next();
            User user1=new User();
            user1.setUsername(username);
            List<User> users = userMapper.selectByConditionSingle(user1);
            if (users.size()==0)break;
            else {
                System.out.println("👿该用户已存在，请重新输入");
            }
        }
        while (true) {
            System.out.println("🙈请输入你的密码：");
            String firstPassword= sc.next();
            System.out.println("🙈请再次输入你的密码：");
            String secondPassword= sc.next();
            if (!firstPassword.equalsIgnoreCase(secondPassword)){
                System.out.println("👿两次密码不一致，请重新输入");
            }else {
                password=secondPassword;
                break;
            }
        }
        System.out.println("🐵请输入性别：");
        gender= sc.next();
        System.out.println("🐵请输入联系方式：");
        contactInformation= sc.next();
        System.out.println("🐵请输入个性签名：");
        signature= sc.next();
        user.setUsername(username);
        user.setPassword(password);
        user.setGender(gender);
        user.setContactInformation(contactInformation);
        user.setSignature(signature);



        userMapper.add(user);
//提交事务
        if (user.getId()>0)System.out.println("🐵亲爱的"+user.getUsername()+"恭喜你注册成功！");
        else System.out.println("😶发生了未知错误注册失败");



        sqlSession.commit();

        sqlSession.close();



    }
    public static void login() throws IOException {
        String username = "";
        String password="";
        System.out.println("🐵请输入用户名：");
        username= sc.next();
        System.out.println("🙈请输入你的密码：");
        password= sc.next();
        User user=new User();
        user.setUsername(username);
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();


        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.selectByConditionSingle(user);
        if (users.size()==0) {
            System.out.println("👿找不到该用户，登录失败");
            return;
        }
        for (User u:users){
            if (u.getPassword().equalsIgnoreCase(password))System.out.println("🐵亲爱的"+u.getUsername()+"欢迎回来！");
            else System.out.println("👿用户名或密码错误，登录失败");


        }
    }
}
